package games.charly
{
	import org.flixel.FlxSprite;
	import org.flixel.FlxG;
	
	public class CharlyChar extends FlxSprite
	{
		[Embed(source = "../../../assets/charly/CH1_A.png")] private var img0:Class;
		[Embed(source = "../../../assets/charly/CH2_A.png")] private var img1:Class;
		[Embed(source = "../../../assets/charly/CH4_A.png")] private var img2:Class;
		[Embed(source = "../../../assets/charly/CH4_B.png")] private var img3:Class;
		[Embed(source = "../../../assets/charly/CH4_C.png")] private var img4:Class;
		[Embed(source = "../../../assets/charly/CH5.png")] private var imgGrave:Class;
		[Embed(source = "../../../assets/charly/CH3_A.png")] private var imgEnding:Class;
		[Embed(source = "../../../assets/charly/Zambullido0.png")] private var imgWater0:Class;
		[Embed(source = "../../../assets/charly/Zambullido1.png")] private var imgWater1:Class;
		[Embed(source = "../../../assets/charly/Zambullido2.png")] private var imgWater2:Class;
		[Embed(source = "../../../assets/charly/Zambullido3.png")] private var imgWater3:Class;
		
		//public var balcon:Boolean;
		private var state:String;
		
		private var roofX:int;
		private var roofY:int;
		private var floorY:int;
		private var poolX:int;
		
		private var velX:Number;
		private var velY:Number;
		private var animationVelX:Number;
		private var animationVelY:Number;
		private var contWater:int;
		
		// esta clase no la entiendo...
		// contame que te imaginas...
		// ahi borre las 6 variables booleanas que armaban el quilombo y pase todo a un diagrama de estados
		
		public function CharlyChar(_x:Number=0, _y:Number=0, _floorY:Number=0, _poolX:Number=0)
		{
			super(FlxG.width - 60, _y - 46, img0);
			x -= int(width / 2);
			
			roofX = _x;
			roofY =  _y - 46;
			floorY = _floorY;
			poolX = _poolX;
			
			width = 28;
			height = 47;
			centerOffsets();
			
			health = 4;
			
			state = "startAnimation";
			
			contWater = 0;
			
			animationVelX = 1.5;//1.5
			animationVelY = 1;
			velX = 100;//1
			velY = 100;//1
		}
		
		override public function update():void
		{
			if (state == "startAnimation")
			{
				if (x > roofX)
				{
					x -= 1.5;
				}
				else
				{
					x -= animationVelX;
					
					if (y > roofY - 50)
					{
						y -= animationVelY;
						animationVelY -= 0.01;
					}
					else
					{
						state = "startFalling";
						loadGraphic(img1);
						width = 28;
						height = 47;
						centerOffsets();
						
					}
				}
			}
			else if (state == "startFalling")
			{
				x -= animationVelX;
				y += animationVelY;
				animationVelX -= 0.01;
				animationVelY += 0.05;
				if (y > roofY - 0)
				{
					state = "playing";
				}
			}
			else if (state == "playing" || state == "playingEnding" || state == "won")
			{
				if (state != "won")
				{
					if (FlxG.keys.LEFT)
					{
						velocity.x = -velX;
					}
					else if (FlxG.keys.RIGHT)
					{
						velocity.x = velX;
					}
					else
					{
						velocity.x = 0;
					}
						
					if(x > FlxG.width - 173 - width)
						x = FlxG.width - 173 - width;
					if(x < 0)
						x = 0;
						
					velocity.y = velY;
					
					//if (!balcon)
					//{
						//y += velY;
						if (velY < 420)
						{
							velY += 0.5;
						}
					//}
					//else
					//{
						//velY = 0;
					//}
				}
				
				if (y > floorY - 280)
				{
					if (state == "playing")
					{
						velocity.x = 0;
						state = "playingEnding";
						loadGraphic(imgEnding);
						width = 28;
						height = 47;
						centerOffsets();
					}
					else if(state == "playingEnding" || state == "won")
					{
						if (y > floorY + 40)
						{
							if (x < poolX)
							{
								state = "won";
								velocity.y = 0;
								
								if (contWater == 0)
								{
									loadGraphic(imgWater0);
									width = 28;
									height = 47;
									offset.x = (frameWidth - width) * 0.5;
									offset.y = frameHeight - height/2;
								}
								else if (contWater == 5)
								{
									loadGraphic(imgWater1);
									width = 28;
									height = 47;
									offset.x = (frameWidth-width)*0.5;
									offset.y = frameHeight - height/2;
								}
								else if (contWater == 10)
								{
									loadGraphic(imgWater2);
									width = 28;
									height = 47;
									offset.x = (frameWidth-width)*0.5;
									offset.y = frameHeight - height/2;
								}
								else if (contWater == 15)
								{
									loadGraphic(imgWater3);
									width = 28;
									height = 47;
									offset.x = (frameWidth-width)*0.5;
									offset.y = frameHeight - height/2;
								}
								else if (contWater == 20)
								{
									(FlxG.state as CharlyGame).endGame(true);
									kill();
								}
								contWater++;
							}
							else
							{
								state = "dead";
							}
						}
					}
				}
			}
			else if (state == "dead")
			{
				velocity.x = 0;
				
				if (y < floorY - 20)
				{
					//y += velY;
					velocity.y = velY;
					
					velY += 10;
				}
				else
				{
					velocity.y = 0;
					state = "grave";
					y = floorY - 20;
					angle = 0;
					loadGraphic(imgGrave);
					centerOffsets();
					(FlxG.state as CharlyGame).endGame(false);
				}
			}
			
			//trace(state);
			
			super.update();
		}
		
		override public function hurt(Damage:Number):void 
		{
			//super.hurt(Damage);
			if (state == "playing")
			{
				health = health - Damage;
				
				updateGraphic();
				
				if (health <= 0)
				{
					state = "dead";
					angle = 180;
				}
			}
		}
		
		public function recover(amount:Number):void
		{
			if (state == "playing" && health < 4)
			{
				health = health + amount;
				
				updateGraphic();
			}
		}
		
		public function updateGraphic():void
		{
			switch (health)
			{
				case 4:
					loadGraphic(img1);
					break;
				case 3:
					loadGraphic(img2);
					break;
				case 2:
					loadGraphic(img3);
					break;
				case 1:
					loadGraphic(img4);
					break;
			}
			width = 28;
			height = 47;
		}
	}
}